package com.sskj.gcc.ui.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.box.imageloader.GlideImageLoader;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.LanguageUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.BannerBean;
import com.sskj.gcc.bean.ZixunListBean;
import com.sskj.gcc.http.HttpConfig;
import com.sskj.gcc.presenter.ZixunActivityPresenter;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.APP_ZIXUN)
public class ZixunActivity extends BaseActivity<ZixunActivityPresenter> {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.ivBanner)
    Banner ivBanner;
    @BindView(R.id.ivBack)
    LinearLayout ivBack;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_zixun;
    }

    @Override
    public ZixunActivityPresenter getPresenter() {
        return new ZixunActivityPresenter();
    }

    @Override
    protected void initView() {
        tvTitle.setText(App.INSTANCE.getString(R.string.appJavaZixunFragment1));
        ClickUtil.click(ivBack, new ClickUtil.Click() {
            @Override
            public void click() {
                finish();
            }
        });
        ivBanner.setImageLoader(new GlideImageLoader());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 10))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.app_divider))
        );
        SlimAdapter<ZixunListBean> slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_zixun, new SlimInjector<ZixunListBean>() {
            @Override
            public void onInject(ZixunListBean data, IViewInjector injector, List payloads) {
                injector
                        .with(R.id.ivImg, view -> {
                            ImageView imageView = (ImageView) view;
                            ImageUtil.setRoundImage(data.getPic_addr(), imageView);
                        })
                        .text(R.id.tvTitle, data.getBm_title())
                        .text(R.id.tvTime, data.getIssue_time());

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<ZixunListBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<ZixunListBean>)
                page -> mPresenter.getData(page + ""), 20);
        MVCCoolHelper<List<ZixunListBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener((recyclerView1, position, v) -> {
                    ARouter.getInstance()
                            .build(RConfig.APP_NOTICE_DETAIL)
                            .withString(Constans.HEAD, App.INSTANCE.getString(R.string.appJavaZixunListFragment2))
                            .withString(Constans.CONTENT, slimAdapter.getDataItem(position).getContent())
                            .withString(Constans.TIME, slimAdapter.getDataItem(position).getIssue_time())
                            .withString(Constans.TITLE, slimAdapter.getDataItem(position).getBm_title())
                            .navigation();
                });
    }

    @Override
    protected void initData() {
        if (LanguageUtil.isChinese()) {
            mPresenter.getBanner("1");
        }else {
            mPresenter.getBanner("5");
        }
    }

    public void updateUI(List<BannerBean> data) {
        if (data == null || ivBanner == null)
            return;
        Flowable.fromIterable(data)
                .map(bannerBean -> HttpConfig.BASE_URL + bannerBean.getPath())
                .toList()
                .subscribe(strings -> {
                    ivBanner.setImages(strings);
                    ivBanner.setOnBannerListener(position -> {
//                        ARouter.getInstance().build(RConfig.APP_GUIDE_WEB)
//                                .withString(Constans.URL, data.get(position).getUrl())
//                                .navigation();
                    });
                    ivBanner.start();
                });
    }

}
